package com.tengju.data.infrastructure.repository.wx;

import com.tengju.data.domain.model.wx.WxRequestRecord;
import com.tengju.data.domain.model.wx.WxRequestRecordRepository;
import com.tengju.data.domain.model.wx.WxRequestRecordStatus;
import com.tengju.data.domain.model.wx.WxRequestRecordType;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.WxRequestRecordDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.tengju.custom.WxRequestRecordDOCustomMapper;
import com.tengju.data.infrastructure.repository.convert.WxRequestRecordConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 李大仙
 * @date: 2021/11/7 5:18 下午
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class WxRequestRecordRepositoryImpl implements WxRequestRecordRepository {

    private final WxRequestRecordDOCustomMapper wxRequestRecordDOCustomMapper;

    @Override
    public int batchInsert(List<WxRequestRecord> wxRequestRecordList) {
        List<WxRequestRecordDO> wxRequestRecordDOList = wxRequestRecordList.stream()
                .map(WxRequestRecordConvert::convert)
                .collect(Collectors.toList());
        return wxRequestRecordDOCustomMapper.batchInsert(wxRequestRecordDOList);
    }

    @Override
    public List<WxRequestRecord> selectByStatus(List<WxRequestRecordType> wxRequestRecordTypeList,
                                                WxRequestRecordStatus wxRequestRecordStatus,
                                                String orderBy) {
        List<Integer> typeList = wxRequestRecordTypeList.stream()
                .map(WxRequestRecordType::getCode)
                .collect(Collectors.toList());
        List<WxRequestRecordDO> wxRequestRecordDOList = wxRequestRecordDOCustomMapper.selectByStatus(typeList, wxRequestRecordStatus.getCode(), orderBy);
        return wxRequestRecordDOList.stream()
                .map(WxRequestRecordConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public int update(WxRequestRecord wxRequestRecord, WxRequestRecordStatus wxRequestRecordStatus) {
        WxRequestRecordDO wxRequestRecordDO = WxRequestRecordConvert.convert(wxRequestRecord);
        return wxRequestRecordDOCustomMapper.update(wxRequestRecordDO, wxRequestRecordStatus.getCode());
    }
}
